package com.ntanh.mealplanner.main.objects;

import android.content.ContentValues;
import android.util.Log;

import com.google.android.gms.drive.internal.m;
import com.ntanh.mealplanner.base.model.BaseEntity;
import com.ntanh.mealplanner.main.dao.BaseDao;
import com.ntanh.mealplanner.main.dao.DataBaseHelper;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Anh Nguyen on 2/24/2015.
 */
public class DayMeal extends BaseEntity {

    public static final String MEAL_LIST = "mealList";

    public static final String DATE = "date";

    private List<Meal> mealList;

    private Date date;

    public DayMeal(String author) {
        super(author);
        date = new Date();
        mealList = new ArrayList<Meal>();
    }

    public DayMeal(String author, Date date) {
        super(author);
        this.date = date;
        mealList = new ArrayList<Meal>();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof DayMeal)) return false;
        if (!super.equals(o)) return false;

        DayMeal dayMeal = (DayMeal) o;

        if (mealList != null && dayMeal.mealList != null && mealList.size() == dayMeal.mealList.size()) {
            for (int i = 0; i < mealList.size(); i++) {
                if (!mealList.get(i).equals(dayMeal.mealList.get(i))) {
                    return false;
                }
            }
        } else {
            return false;
        }
        if (!mealList.equals(dayMeal.mealList)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        if (mealList != null) {
            int hash = 0;
            for (Meal meal : mealList) {
                hash += meal.hashCode();
            }
            result = 31 * result + hash;
        }
        return result;
    }

    public List<Meal> getMealList() {
        return mealList;
    }

    public void setMealList(List<Meal> mealList) {
        this.mealList = mealList;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public JSONObject toJSONObject() {

        JSONObject json = super.toJSONObject();
        try {
            if (json != null) {
                json = new JSONObject();
            }

            if (mealList != null) {
                JSONArray array = new JSONArray();

                for (Meal meal : mealList) {
                    array.put(meal.toJSONObject());
                }

                json.put(DayMeal.MEAL_LIST, array);
            }

            json.put(DayMeal.DATE, date);

        } catch (Exception e) {
            Log.d("Convert error", "Cannot convert DayMeal to JSON", e);
        }

        return json;
    }

    public void toContentValue(ContentValues values, boolean forInserting) {
        super.toContentValue(values, forInserting);
        values.put(DATE, BaseEntity.dateFormatter.format(getDate()));
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mealList.size() - 1; i++) {
            sb.append(mealList.get(i).getId());
            sb.append(BaseEntity.entitySplitSign);
        }
        if (mealList.size() > 0) {
            sb.append(mealList.get(mealList.size() - 1).getId());
        }
        values.put(MEAL_LIST, sb.toString());
    }

    @Override
    public String getTableName() {
        return DataBaseHelper.DAY_MEAL_TABLE;
    }
}
